//
//  UserAccount.swift
//  WBDemo
//
//  Created by Jin on 23/04/2018.
//  Copyright © 2018 com.baidu.123. All rights reserved.
//



//KVC为OC运行时机制

 import Foundation
 class UserAccount: NSObject,NSCoding {
 
     /*
     这是因为KVC是OC的方法,OC方法和属性都需要运行时写到类对象中，
     swift利用KVC设置属性需要运行时机制,
     swift4.0以前，编译器默认帮我们做了在对象声明前加上了@objc，4.0需要手动加上。
     */
        @objc var expires_in:NSNumber?{
            didSet{
                expires_date = Date(timeIntervalSinceNow: Double(truncating: expires_in!))
            }
        }
        @objc var access_token:String?
        @objc var remind_in:String?
        @objc var isRealName:String?
        @objc var uid:String?
        //服务器返回过期时间为时间间隔,故设置此属性存储可使用的过期时间
        @objc var expires_date:Date?
        //昵称
        @objc var screen_name:String?
        //头像地址
        @objc var avatar_large:String?
    
        static var userAccount:UserAccount?
    
     init(dict:[String:AnyObject]){
        super.init()
        setValuesForKeys(dict)
     }
    
     override func setValue(_ value: Any?, forUndefinedKey key: String) {}
     override func setValue(_ value: Any?, forKey key: String) {
        super.setValue(value, forKey: key)
     }
    
     override var description: String {
        let keys = ["access_token","expires_date", "remind_in","isRealName","uid","screen_name","avatar_large"]
        return dictionaryWithValues(forKeys: keys).description
        }
    
    // MARK: - 外部控制方法
    //保存模型到本地
    func saveAccount() {
        NSKeyedArchiver.archiveRootObject(self, toFile: "userAccount.plist".docDir())
    }
    
    //从本地读取模型
    class func loadUserAccount() -> UserAccount?{
        
        //1.判断是否已经加载过了
        if userAccount != nil {
            return userAccount
        }
        
        //2.未加载过
        userAccount = NSKeyedUnarchiver.unarchiveObject(withFile: "userAccount.plist".docDir()) as? UserAccount
        
        //3.判断是否过期
        if let date = userAccount?.expires_date, date.compare(Date()) == ComparisonResult.orderedAscending {
            JLog(message: "授权过期")
            userAccount = nil
            return userAccount
        }
        JLog(message: "授权正常")
        return userAccount
    }
    
    //根据有无保存模型判断是否登录
    class func login() -> Bool{
        return UserAccount.loadUserAccount() != nil
    }
    
    // MARK: - NSCoding
    //解归档
    required init?(coder aDecoder: NSCoder) {
        expires_in = aDecoder.decodeObject(forKey: "expires_in") as? NSNumber
        access_token = aDecoder.decodeObject(forKey: "access_token") as? String
        remind_in = aDecoder.decodeObject(forKey: "remind_in") as? String
        isRealName = aDecoder.decodeObject(forKey: "isRealName") as? String
        uid = aDecoder.decodeObject(forKey: "uid") as? String
        screen_name = aDecoder.decodeObject(forKey: "screen_name") as? String
        avatar_large = aDecoder.decodeObject(forKey: "avatar_large") as? String
        expires_date = aDecoder.decodeObject(forKey: "expires_date") as? Date
    }
    //归档
    func encode(with aCoder: NSCoder) {
        aCoder.encode(access_token, forKey: "access_token")
        aCoder.encode(expires_in, forKey: "expires_in")
        aCoder.encode(remind_in, forKey: "remind_in")
        aCoder.encode(isRealName, forKey: "isRealName")
        aCoder.encode(uid, forKey: "uid")
        aCoder.encode(screen_name, forKey: "screen_name")
        aCoder.encode(avatar_large, forKey: "avatar_large")
        aCoder.encode(expires_date, forKey: "expires_date")
    }
    
}




